package org.tio.sitexxx.service.service.qj;

import com.alibaba.fastjson.JSON;
import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.sitexxx.service.model.main.Wardrobe;
import org.tio.sitexxx.service.vo.Const;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WardrobeService {//用户衣橱表
    private static Logger log	= LoggerFactory.getLogger(WardrobeService.class);
    public static final WardrobeService me	= new WardrobeService();
    final Wardrobe dao	= new Wardrobe().dao();
    public WardrobeService(){

    }

    /**
     * 返回所有用户添加的衣橱
     * @param query
     * @return
     */
    public List<Wardrobe> getWardrobes(Map<String,Object> query){
        List<Wardrobe>ResultList=new ArrayList<>(6);

        String sqlid = "wardrobe.getWardrobes";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
//        Member list = Member.dao.findFirst(sql, openid);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        List<Record> record = Db.use(db).find(sqlPara);
        for(Record record1:record){
            ResultList.add(JSON.parseObject(JSON.toJSONString(record1.getColumns()),Wardrobe.class));

        }
        return ResultList;
    }

    /**
     * save
     * @param wardrobe
     * @return
     */
    public boolean create(Wardrobe wardrobe){

        return wardrobe.save();
    }

    /**
     * 返回单个衣橱
     * @param query
     * @return
     */
    public Wardrobe getWardrobe(Map<String,Object> query){
        String sqlid = "wardrobe.getWardrobe";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
//        Member list = Member.dao.findFirst(sql, openid);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        Record record = Db.use(db).findFirst(sqlPara);
        Wardrobe wardrobe=JSON.parseObject(JSON.toJSONString(record.getColumns()),Wardrobe.class);
        return wardrobe;

    }

    /**
     *用户没有形象测试顾问无法评价衣橱
     * @param memberId
     * @return
     */
    @SuppressWarnings("unchecked")
    public Map<String,Object> getMemberAndWardrobr(int memberId){
        String sql="select m.*,i.id as ImageId,i.style,i.colour,i.shape,i.occasion,i.hobbies,i.createdAt as ImgCreatedAt,i.updatedAt as ImgUpdateAt,i.memberId from qj_main.member m  , qj_main.imagetesting i where  i.memberId=m.id  and m.id=? order by m.updatedAt desc";
        Record record=Db.use(Const.Db.TIO_SITE_MAIN).findFirst(sql,memberId);
        if(null!=record){
            return JSON.parseObject(JSON.toJSONString(record.getColumns()),Map.class);
        }
        return null;
    }

    /**
     * update   paysubmit  根据entity更新wardrobe表paid字段
     * @param wardrobe
     * @return
     */
    public boolean update(Wardrobe wardrobe){
        return wardrobe.update();
    }

    /**
     * 修锅操作
     * --前人留下的锅，本人负责修好（之前如果用户一次性添加两个或两个以上的衣橱，前端在payinit里只发了一个衣橱的id
     * 导致我在微信的回调里无法更新两条记录的paid字段
     * --故做一次查询再来更新，
     * @param id
     * @param memberId
     * @return
     */
    public List<Wardrobe> getWardrobes_(int id,int memberId){
        Map<String,Object> query=new HashMap<>(4);
        query.put("id",id);
        query.put("memberId",memberId);
        List<Wardrobe>resultList=new ArrayList<>(4);
        String sqlid = "wardrobe.getWardrobes_";
        String db = Const.Db.TIO_SITE_MAIN;
        Kv cond=Kv.create().set(query);
        SqlPara sqlPara = Db.use(db).getSqlPara(sqlid,cond);
        List<Record> record = Db.use(db).find(sqlPara);
        if(record!=null){
            record.forEach((record1 -> {
                resultList.add(JSON.parseObject(JSON.toJSONString(record1.getColumns()),Wardrobe.class));
            }));
        }
       return resultList;
    }

}
